package com.bum.api.core.service;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;

import com.bum.api.core.bo.WelcomeBO;
import com.bum.api.core.exp.IApiService;
import com.bum.api.core.exp.om.Metas;
import com.bum.api.core.exp.om.Params;
import com.bum.api.core.service.process.IApiProcess;
import com.google.gson.Gson;

public class ApiService implements IApiService {
	Logger	log	= Logger.getLogger(getClass());

	WelcomeBO		welcomeBO;
	Gson			gson;

	public void setWelcomeBO(WelcomeBO welcomeBO) {
		this.welcomeBO = welcomeBO;
	}

	/**
	 * JSON PROCESSORS
	 */
	Map<String, IApiProcess>	jsonProcessors;

	public void setJsonProcessors(Map<String, IApiProcess> jsonProcessors) {
		this.jsonProcessors = jsonProcessors;
	}

	@Override
	public String getJson(String path, Params params, Metas meta) {
		if (StringUtils.isBlank(path)) {
			return null;
		}

		try {
//			if (path.startsWith("id/")) {
//				meta.put("isGuestPath", "1");
//			}
			welcomeBO.welcome("json/" + path, params, meta);
		} catch (Exception e) {
			return "{apiError:\"" + e.getMessage() + "\"}";
		}

		String[] parts = StringUtils.split(path, "/", 2);

		IApiProcess apiProcess = jsonProcessors.get(parts[0]);
		if (apiProcess == null) {
			return "{apiError:\"Invalid Service\"}";
		}

		String json = apiProcess.process(path, params, meta);
		return checkCallback(params, json);
	}

	private String checkCallback(Params params, String json) {
		String callback = params.getValue("callback");
		if (StringUtils.isNotBlank(callback)) {
			return callback + "(" + json + ")";
		}

		return json;
	}

	/**
	 * HTML PROCESSORS
	 */
	Map<String, IApiProcess>	htmlProcessors;
	IApiProcess					htmlProcessorDefault;

	public void setHtmlProcessors(Map<String, IApiProcess> htmlProcessors) {
		this.htmlProcessors = htmlProcessors;
	}

	public void setHtmlProcessorDefault(IApiProcess htmlProcessorDefault) {
		this.htmlProcessorDefault = htmlProcessorDefault;
	}

	@Override
	public String getHtml(String path, Params params, Metas metas) {
		if (StringUtils.isBlank(path)) {
			return null;
		}

		try {
//			if (path.startsWith("id/")) {
//				metas.put("isGuestPath", "1");
//			}
			welcomeBO.welcome("app/" + path, params, metas);
		} catch (Exception e) {
//			log.debug(e.getMessage() + " " + path + " " + gson.toJson(params) + " " + gson.toJson(metas), e);
			return e.getMessage();
		}

		/**
		 * Thông tin bổ sung
		 */
		int actionBarHeight = NumberUtils.toInt(params.getValue("acbh"), -1);
		if (actionBarHeight > 0) {
			metas.put(Metas.ACTION_BAR_HEIGHT, Integer.toString(actionBarHeight));
		}

		int bottomTabHeight = NumberUtils.toInt(params.getValue("btbh"), -1);
		if (bottomTabHeight > 0) {
			metas.put(Metas.BOTTOM_TAB_HEIGHT, Integer.toString(bottomTabHeight));
		}

		if ("1".equals(params.getValue("ajax"))) {
			metas.put("ajax", "1");
		}

		String[] parts = StringUtils.split(path, "/", 2);

		IApiProcess apiProcess = htmlProcessors.get(parts[0]);
		if (apiProcess == null) {
			apiProcess = htmlProcessorDefault;
		}

		String html = null;
		try {
			html = apiProcess.process(path, params, metas);
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
			return "Invalid service";
		}

		if (StringUtils.isNotBlank(html) && params.getValue("-not-trim-space") == null) {
			html = html.replaceAll("\\s+", " ").trim();
		}
		return html;
	}

	/**
	 * HTML PROCESSORS
	 */
	Map<String, IApiProcess>	pageProcessors;

	public void setPageProcessors(Map<String, IApiProcess> pageProcessors) {
		this.pageProcessors = pageProcessors;
	}

	@Override
	public String getPage(String path, Params params, Metas meta) {
		if (StringUtils.isBlank(path)) {
			return null;
		}

		String[] parts = StringUtils.split(path, "/", 2);

		IApiProcess apiProcess = pageProcessors.get(parts[0]);
		if (apiProcess == null) {
			return "Invalid Service";
		}

		String html = apiProcess.process(path, params, meta);
		if (StringUtils.isNotBlank(html)) {
			html = html.replaceAll("\\s+", " ").trim();
		}
		return html;
	}

	public void setGson(Gson gson) {
		this.gson = gson;
	}

}
